﻿using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CadDotNetTools
{
    public static class CuverTools
    {
        /// <summary>
        /// 偏移曲线
        /// </summary>
        /// <param name="curves">偏移曲线</param>
        /// <param name="dis">偏移距离</param>
        /// <returns>偏移后对象集合</returns>
        public static List<Curve> OffSetLine(List<Curve> curves, double dis)
        {
            List<Curve> curveList = new List<Curve>();
            for (int i = 0; i < curves.Count; i++)
            {
                DBObjectCollection offCur = curves[i].GetOffsetCurves(dis);
                foreach (var c in offCur)
                {
                    Curve curve = (Curve)c;
                    curveList.Add(curve);
                }
            }
            return curveList;
        }
        /// <summary>
        /// 绘制直线
        /// </summary>
        /// <param name="db">图形数据库</param>
        /// <param name="startPoint">起点坐标</param>
        /// <param name="length">直线长度</param>
        /// <param name="degree">与x轴正方向的角度</param>
        /// <returns>ObjectId</returns>
        public static ObjectId AddLineToModelSpace(this Database db, Point3d startPoint, double length, double degree)
        {
            //计算终点坐标
            double x = startPoint.X + length * Math.Cos(degree.DegreeToAngle());
            double y = startPoint.Y + length * Math.Sin(degree.DegreeToAngle());
            Point3d endPoint = new Point3d(x, y, 0);
            return db.AddEntityToModelSpace(new Line(startPoint, endPoint));
        }
        /// <summary>
        /// 顶点创建多段线
        /// </summary>
        /// <param name="vertex2ds">顶点</param>
        /// <param name="closed">是否闭合</param>
        /// <returns>多段线</returns>
        public static Polyline GetPolyline(ICollection<Point2d> vertex2ds, bool closed = true)
        {
            var polyline = new Polyline() { Closed = closed };
            var list = vertex2ds.ToList();
            for (int i = 0; i < list.Count; i++)
            {
                polyline.AddVertexAt(i, list[i], 0, 0, 0);
            }
            return polyline;
        }
        /// <summary>
        /// 获得直线的起点坐标
        /// </summary>
        /// <param name="lineId">直线对象的ObjectId</param>
        /// <returns></returns>
        public static Point3d GetLineStartPoint(this ObjectId lineId)
        {
            Point3d startPoint;
            Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                Line line = (Line)lineId.GetObject(OpenMode.ForRead);
                startPoint = line.StartPoint;
            }
            return startPoint;
        }
        /// <summary>
        /// 获取多段线顶点 并用Text标记
        /// </summary>
        /// <param name="doc">Document</param>
        /// <param name="polyline">多段线</param>
        public static List<Point3d> GetPolylineVertexPointShowText(this Document doc, Polyline polyline)
        {
            var ed = doc.Editor;
            var pointList = new List<Point3d>();
            var ids = new ObjectIdCollection();
            for (int i = 0; i < polyline.EndParam; i++)
            {
                var vertex = polyline.GetPoint3dAt(i);
                var txt = new MText()
                {
                    Location = vertex,
                    TextHeight = 0.03 * (polyline.Length / polyline.EndParam),
                    Contents = $"点{i}",
                    ColorIndex = i + 1,
                    Width = 0,  //设置为0表示不会自动换行
                };
                pointList.Add(vertex);
                doc.Database.AddEntityToModelSpace(txt);
                ids.Add(txt.ObjectId);
            }
            doc.Database.CreateGroup("每个顶点de", ids);
            ed.WriteMessage($"\n 有{polyline.EndParam}个顶点.");
            return pointList;
        }
        /// <summary>
        /// 绘制直线
        /// </summary>
        /// <param name="db">图形数据库</param>
        /// <param name="startPoint">起点坐标</param>
        /// <param name="endPoint">终点坐标</param>
        /// <returns>ObjectId</returns>
        public static ObjectId AddLineToModelSpace(this Database db, Point3d startPoint, Point3d endPoint)
        {
            return db.AddEntityToModelSpace(new Line(startPoint, endPoint));
        }
        /// <summary>
        /// 绘制圆弧
        /// </summary>
        /// <param name="db">图形数据库</param>
        /// <param name="center">圆弧所在圆的圆心点</param>
        /// <param name="radius">圆弧的半径</param>
        /// <param name="startDegree">圆弧的起始角度</param>
        /// <param name="endDegree">圆弧的终止角度</param>
        /// <returns>ObjectId</returns>
        public static ObjectId AddArcToModelSpace(this Database db, Point3d center, double radius, double startDegree, double endDegree)
        {
            return db.AddEntityToModelSpace(new Arc(center, radius, startDegree.DegreeToAngle(), endDegree.DegreeToAngle()));
        }
        /// <summary>
        /// 创建XLine
        /// </summary>
        /// <param name="pointBase">第一个点</param>
        /// <param name="pointSecond">第二个点</param>
        /// <returns></returns>
        public static Xline CreateXline(this Point3d pointBase, Point3d pointSecond)
        {
            var xline = new Xline();
            xline.BasePoint = pointBase;
            xline.SecondPoint = pointSecond;
            return xline;
        }
    }
} 
